unit ddldump_;

{$DEFINE Delphi}
interface
uses
  Classes
  , SysUtils
  ;

type
{$IFDEF Delphi}
  TCustomApplication=class(TComponent)
  private
    FTitle: string;
    FStopOnException: Boolean;
    procedure SetTitle(const Value: string);
  protected
    procedure DoRun; virtual; abstract;
  public
    procedure Run;
    procedure Terminate;

    function HasOption(const AOption1, AOption2: string): Boolean;
    function CheckOptions(const AOption1, AOption2: string): string; virtual;
    procedure ShowException(AException: Exception);

    property Title: string read FTitle write SetTitle;
    property StopOnException: Boolean read FStopOnException write FStopOnException;
  end;
{$ENDIF}

  { TDdlDump }
  TDdlDump = class(TCustomApplication)
  protected
    procedure DoRun; override;
    procedure ParseDb;
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    procedure WriteHelp; virtual;
  end;

implementation

{$IFDEF Delphi}

uses
  dbentities
  , mssql
  ;

{ TCustomApplication }

function TCustomApplication.CheckOptions(const AOption1,
  AOption2: string): string;
begin

end;

function TCustomApplication.HasOption(const AOption1,
  AOption2: string): Boolean;
begin
  Result := False;
end;

procedure TCustomApplication.Run;
begin
  DoRun;
end;

procedure TCustomApplication.SetTitle(const Value: string);
begin
  FTitle := Value;
end;
procedure TCustomApplication.ShowException(AException: Exception);
begin

end;

procedure TCustomApplication.Terminate;
begin
  Halt;
end;

{$ENDIF}

{ TDdlDump }

procedure TDdlDump.DoRun;
var
  ErrorMsg: String;
begin
  // quick check parameters
  ErrorMsg:=CheckOptions('h','help');
  if ErrorMsg<>'' then begin
    ShowException(Exception.Create(ErrorMsg));
    Terminate;
    Exit;
  end;

  // parse parameters
  if HasOption('h','help') then begin
    WriteHelp;
    Terminate;
    Exit;
  end;

  { add your program here }
  ParseDb;

  // stop program loop
  Terminate;
end;

procedure TDdlDump.ParseDb;
var
  i: Integer;
  vDb: TDbDatabase;
  vLines: TStrings;
  vDbInfo: TDbHostInfo;
begin
  vDbInfo.DbName:='SisTO';
  vDbInfo.Host:='SAWO';
  vDbInfo.UsrName:='sa';
  vDbInfo.UsrPass:='masterkey';

  with TMsSqlParser.Create do
  begin
    Parse([], vDbInfo, vDb);
    vLines := TStringList.Create;
    try
      GetCreateDDLs(vDb, vLines);

      for i := 0 to vLines.Count-1 do
        WriteLn(vLines[i]);
    finally
      vLines.Free;
    end;
    Exit;


    for i := 0 to vDb.Tables.Count-1 do
    begin
      WriteLn(vDb.Tables[i].Name + ': ' + '');
      WriteLn(vDb.Tables[i].CreateDDL);
      WriteLn('GO');
      WriteLn('');
    end;
    for i := 0 to vDb.Views.Count-1 do
    begin
      WriteLn(vDb.Views[i].Name + ': ' + '');
      WriteLn(vDb.Views[i].DDL);
      WriteLn('GO');
      WriteLn('');
    end;
    for i := 0 to vDb.Procs.Count-1 do
    begin
      WriteLn(vDb.Procs[i].Name + ': ' + '');
      WriteLn(vDb.Procs[i].DDL);
      WriteLn('GO');
      WriteLn('');
    end;
    for i := 0 to vDb.UDFs.Count-1 do
    begin
      WriteLn(vDb.UDFs[i].Name + ': ' + '');
      WriteLn(vDb.UDFs[i].DDL);
      WriteLn('GO');
      WriteLn('');
    end;
    vDb.Free;
  end;
end;

constructor TDdlDump.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  StopOnException:=True;
end;

destructor TDdlDump.Destroy;
begin
  inherited Destroy;
end;

procedure TDdlDump.WriteHelp;
begin
  { add your help code here }
  writeln('Usage: ', ParamStr(0),' -h');
end;

end.
